/**
 * 证书编辑
 * @param id
 */
function editCertificate(id) {
    DGG.loading();
    var adminEdit = $("[name='adminEdit']").val();
    if (checkIsEmpty(adminEdit)) {
        $.post(sysInfo.basePath + "/zs/general_credential/check_certificate.do", {
            id: id,
            libraryStatus: 'GENERAL_CREDENTIAL_LIBRARY_STATUS_02'
        }, function (result) {
            if (result.code == 0) {
                editCretificate(id);
            } else {
                DGG.Errors(result.msg);
                DGG.loading('close');
                return false;
            }
        });
    } else {
        editCretificate(id);
    }

}

function checkCertificateFiled() {
    if (!$('select[name=isTalentOriginalCopyCoordinate]').val()){
        DGG.Tip("请选择人才原件是否配合",$('select[name=isTalentOriginalCopyCoordinate]'),{tips: [3, '#000'], time: 3000});
        return false;
    }
    // debugger
    if (!$('select[name=isTalentAppearanceCoordinate]').val()){
        DGG.Tip("请选择人才出场是否配合",$('select[name=isTalentAppearanceCoordinate]'),{tips: [3, '#000'], time: 3000});
        return false;
    }
    // 验证下拉选择框
    if (!$('select[name=talentPaymentMethod]').val() ) {
        DGG.Tip("请选择人才付款方式",$('select[name=talentPaymentMethod]'),{tips: [3, '#000'], time: 3000});
        return false;
    }
    if (!$('select[name=receiptType]').val() ) {
        DGG.Tip("请选择收证类型",$('select[name=receiptType]'),{tips: [3, '#000'], time: 3000});
        return false;
    } else if ($('select[name=receiptType]').val() === '0'){
        if (!checkInputBox('#channelName')) return false;
        if (!checkInputBox('#channelPhone')) return false;
        if (!checkInputBox('#channelIdCard')) return false;
    }
    if (!$('select[name=socialSecurity]').val()){
        DGG.Tip("请选择是否购买社保",$('select[name=socialSecurity]'),{tips: [3, '#000'], time: 3000});
        return false;
    }
    if ($('select[name=socialSecurity]').val() === '0' ) {
        if (!$("input[name=socialSecurityAreaChange]").val()){
            DGG.Tip("请选择社保区域",$('input[name=socialSecurityAreaChange]'),{tips: [3, '#000'], time: 3000});
            return false;
        }
    }
    // 验证下拉选择框
    if (!checkDropDownChoice('#certificateTypes', $('#certificateTypes').next(), '')) return false;
    if (!checkDropDownChoice('#certificateSpecialty', $('#certificateSpecialty').next())) return false;
    if (!checkCertificateSpecialty('#certificateTypes', '#certificateSpecialty')) return false;
    if (!checkDropDownChoice('#certificateLevels', $('#certificateLevels').next())) return false;
    if (!checkInputBox('#contractSigningTimes')) return false;

    // 验证输入框
    if (!checkInputBox('#certificateName', '请选择地区')) return false;
    if (!isFloatGteZeroByLength('#receiptPrices', 2, true, 8)) return false;
    if (!checkInputBox('#personalNames')) return false;
    if (!checkInputBox('#certificateNames')) return false;
     var oldPersonalPhone = $('[name="oldPersonalPhone"]').val();
     var newPersonalPhone = $('#personalPhones').val();
     //未修改号码不做判断
     if(oldPersonalPhone != newPersonalPhone) {
         if (!isMobile('#personalPhones', true)) return false;
     }
    if (!checkInputBox('#accountNames')) return false;
    if (!checkInputBox('#accountBanks')) return false;
    if (!isIdCardNo("#personalIdCards")) return false;
    if (!isWx('#personalWeixins')) return false;
    if (!regCheck(/^[0-9]\d*$/, '#accountNos', '请输入正确的银行卡号', true)) return false;
    if (!$("#certificateNo").val()) {
        DGG.Warning("证书编号不能为空");
        return false;
    }
    if (!checkCertificateNo("#certificateNo", 'GENERAL_CREDENTIAL', $('[name="id"]').val(), 0)) return false;
    //使用校验
    if ("GENERAL_CREDENTIAL_STATUS_03" === $("#credentialForm [name='certificateStatus']").val()) {
        var affiliatedCompany = $("#credentialForm [name='affiliatedCompany']").val();
        var affiliatedExpireTimes = $("#credentialForm [name='affiliatedExpireTimes']").val();
        var affiliatedStartTimes = $("#credentialForm [name='affiliatedStartTimes']").val();
        var threeAffiliatedCompany = $("#credentialForm [name='threeAffiliatedCompany']").val();
        var threeAffiliatedStartTimes = $("#credentialForm [name='threeAffiliatedStartTimes']").val();
        var threeAffiliatedExpireTimes = $("#credentialForm [name='threeAffiliatedExpireTimes']").val();
        if (affiliatedCompany) {
            if (!affiliatedStartTimes) {
                DGG.Warning("请选择使用起始时间");
                return false;
            }
            if (!affiliatedExpireTimes) {
                DGG.Warning("请选择使用到期时间");
                return false;
            }
        }
        if (threeAffiliatedCompany) {
            if (!threeAffiliatedStartTimes) {
                DGG.Warning("请选择三类使用起始时间");
                return false;
            }
            if (!threeAffiliatedExpireTimes) {
                DGG.Warning("请选择三类使用到期时间");
                return false;
            }
        }
        if ((affiliatedStartTimes || affiliatedExpireTimes) && !affiliatedCompany) {
            DGG.Warning("请填写使用公司");
            return false;
        }
        if ((threeAffiliatedStartTimes || threeAffiliatedExpireTimes) && !threeAffiliatedCompany) {
            DGG.Warning("请填写三类使用公司");
            return false;
        }
        if (!affiliatedCompany && !threeAffiliatedCompany) {
            DGG.Warning("至少填写一种使用类型");
            return false;
        }
    }
    return true;
}


/**
 * 保存证书
 * @param params
 */
function updateCretificate(params) {
    $.post(sysInfo.basePath + "/zs/general_credential/edit_certificate.do", params, function (result) {
        if (result.code == 0) {
            DGG.Succeed('证书修改成功');
            search('#myTable', 'reload');
            setTimeout(function () {
                layer.closeAll();
            }, 500);
        } else {
            DGG.Errors(result.msg);
        }
        DGG.loading('close');
    });
}

/**
 * 显示社保区域
 */
function showSocialSecurityArea() {

    let socialSecurity = $('#socialSecurity').val();
    if (socialSecurity === '0'){
        $("#divSocialSecurityArea").css("display","block")
        $("span[name=isSocialSecurity]").css("display","inline-block")
    } else {
        $("#divSocialSecurityArea").css("display","none")
        $("span[name=isSocialSecurity]").css("display","none")
        $("input[name=socialSecurityAreaId]").val("")
        $("input[name=socialSecurityArea]").val("")
        $("input[name=socialSecurityAreaChange]").val("")
    }
}
function editCretificate(id,productId,talentRenewal) {
    $.post(sysInfo.basePath + "/zs/general_credential/edit_credential_page.do", {id: id,productId: productId,talentRenewal: talentRenewal}, function (result) {
        var index = dgg.Open({
            type: 1,
            title: '证书编辑',
            btn: ["确定", "取消"],
            content: result,
            area: '90%',
            yes: function () {
                DGG.loading();
                // 验证页面字段
                if (!checkCertificateFiled()) {
                    DGG.loading('close');
                    return false;
                }
                // debugger
                // 修改证书
                var params = $("#credentialForm").serializeObject();
                params.certificateSpecialtys = getTextVal('#certificateSpecialty');
                params.certificateSpecialty = getVal('#certificateSpecialty', 0);
                params.certificateTypeId = $("#certificateTypes option:selected").val();
                params.certificateType = $("#certificateTypes option:selected").html();
                params.baseId = $("#id").val();
                params.talentRenewal = $("input[name=talentRenewal]").val();
                updateCretificate(params);
            }, end: function () {
                layer.closeAll("tips");
            }
        });
    });
}